package top.lingkang.authoritypro.config.handler;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.lingkang.authoritypro.data.dto.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lingkang
 * @date 2021/12/18
 * 控制层全局异常捕获！
 */
@Slf4j
@RestControllerAdvice
public class GlobalControllerException {
    @ExceptionHandler(Exception.class)
    public Object exception(Exception e, HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.error(
                String.format("请求路径出现异常：%s 请求参数：%s",
                        request.getServletPath(), JSON.toJSONString(parameterMap)
                ),
                e);
        return new ResponseResult<>().fail(e.getMessage()==null?"java.lang.NullPointerException: null":e.getMessage());
    }
}
